﻿using System;
using System.Net.Sockets;

namespace Caius.Core
{
    /// <summary>
    /// Менеджер приложения для использования глобалных переменных
    /// </summary>
    public sealed class ApplicationManager
    {
        private static volatile ApplicationManager instance;
        private static object syncRoot = new Object();

        private Socket clientSocket;

        private ApplicationManager() 
        {
            this.InitSocket();
        }

        /// <summary>
        /// Экземпляр менеджера
        /// </summary>
        public static ApplicationManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new ApplicationManager();
                    }
                }

                return instance;
            }
        }

        /// <summary>
        /// Сокет соединения с Asterisk
        /// </summary>
        public Socket ClientSocket 
        {
            get
            {
                return clientSocket;
            }
        }

        /// <summary>
        /// Инициализация сокета
        /// </summary>
        public void InitSocket()
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.ReceiveBufferSize = 1024;
            clientSocket.ReceiveTimeout = 1000;
            clientSocket.SendTimeout = 1000;
            clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
        }
    }
}
